package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.RolePermissionMapper;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.atguigu.ssyx.acl.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhangYK
 * @version 8.0
 * @title RolePermissionServiceImpl
 * @description
 * @create 2024/7/6 15:17
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {
    /**
     * 为角色分配菜单权限
     * @param roleId
     * @param permissionId
     */
    @Override
    public void doAssignByRole(Long roleId, Long[] permissionId) {
        lambdaUpdate().eq(RolePermission::getRoleId,roleId)
                .remove();

        List<RolePermission> rolePermissions = new ArrayList<>();
        for (Long perId : permissionId) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(perId);
            rolePermissions.add(rolePermission);
        }
        this.saveBatch(rolePermissions);
    }
}
